﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using DTO;
namespace DAO
{
    public class DiaDiemDAO
    {
        public static List<DiaDiemDTO> danhSachDiaDiemTheoTenQuocGia(String sTenQuocGia, String sStrPath)
        {
            List<DiaDiemDTO> listInfo = new List<DiaDiemDTO>();            
            XmlDocument doc = new XmlDocument();
            doc.Load(sStrPath);
            XmlNode root = doc.DocumentElement;
            for (int i = 0; i < root.ChildNodes.Count; i++)//Duyet qua cac quoc gia
            {
                XmlNode nodeQuocGia = root.ChildNodes[i];
                String pTenQuocGia = nodeQuocGia.Attributes["tenQuocGia"].Value.ToString().ToLower();
                int de = pTenQuocGia.IndexOf(sTenQuocGia);
                if (nodeQuocGia.HasChildNodes == true&&pTenQuocGia.IndexOf(sTenQuocGia)>-1)
                {
                    for (int j = 0; j < nodeQuocGia.ChildNodes.Count; j++)//Duyet qua cac tinh thanh
                    {
                        XmlNode nodeTinhThanh = nodeQuocGia.ChildNodes[j];
                        String pTenTinhThanh = nodeTinhThanh.Attributes["tenTinhThanh"].Value.ToString().ToLower();
                        if (nodeTinhThanh.HasChildNodes == true)
                        {
                            for (int h = 0; h < nodeTinhThanh.ChildNodes.Count; h++)//Duyệt qua hết các quận huyện
                            {
                                XmlNode nodeQuanHuyen = nodeTinhThanh.ChildNodes[h];
                                String pTenQuan = nodeQuanHuyen.Attributes["tenQuan"].Value.ToString().ToLower();
                                if (nodeQuanHuyen.HasChildNodes == true)
                                {
                                    for (int k = 0; k < nodeQuanHuyen.ChildNodes.Count; k++)//Duyệt qua các địa điểm
                                    {
                                        XmlNode nodeDiaDiem = nodeQuanHuyen.ChildNodes[k];
                                        string pTenDiaDiem = nodeDiaDiem.Attributes["tenDiaDiem"].Value.ToString().ToLower();
                                        String pLat = nodeDiaDiem.Attributes["Lat"].Value.ToString().ToLower();
                                        string pLng = nodeDiaDiem.Attributes["Lng"].Value.ToString().ToLower();
                                        String pLoaiThongTin = nodeDiaDiem.Attributes["LoaiThongBao"].Value.ToString().ToLower();
                                        DiaDiemDTO diaDiem = new DiaDiemDTO(pTenDiaDiem, pTenQuan, pLat, pLng, pLoaiThongTin);
                                        String thanhPho = nodeTinhThanh.Attributes["tenTinhThanh"].Value.ToLower();
                                        
                                        listInfo.Add(diaDiem);
                                    }
                                }
                            }
                        }


                    }
                }
            }
            return listInfo;
        }
        public static List<DiaDiemDTO> danhSachDiaDiemTheoQuery(ArrayList listInfoQuery,String sStrPath)
        {
            List<DiaDiemDTO> listInfo = new List<DiaDiemDTO>();
            List<DiaDiemDTO> listInfoTheoTPvaQuanHuyen = new List<DiaDiemDTO>();
            XmlDocument doc = new XmlDocument();
            doc.Load(sStrPath);
            XmlNode root = doc.DocumentElement;
            for (int i = 0; i < root.ChildNodes.Count; i++)//Duyet qua cac quoc gia
            {
                XmlNode nodeQuocGia = root.ChildNodes[i];
                String pTenQuocGia = nodeQuocGia.Attributes["tenQuocGia"].Value.ToString().ToLower();
                
                if (nodeQuocGia.HasChildNodes == true&&pTenQuocGia.IndexOf(listInfoQuery[0].ToString()) > -1)
                {
                    for (int j = 0; j < nodeQuocGia.ChildNodes.Count; j++)//Duyet qua cac tinh thanh
                    {
                        XmlNode nodeTinhThanh = nodeQuocGia.ChildNodes[j];
                        String pTenTinhThanh = nodeTinhThanh.Attributes["tenTinhThanh"].Value.ToString().ToLower();
                        if (nodeTinhThanh.HasChildNodes == true)
                        {
                            for (int h = 0; h < nodeTinhThanh.ChildNodes.Count; h++)//Duyệt qua hết các quận huyện
                            {
                                XmlNode nodeQuanHuyen = nodeTinhThanh.ChildNodes[h];
                                String pTenQuan = nodeQuanHuyen.Attributes["tenQuan"].Value.ToString().ToLower();
                                //Thuc hien kiem tra ton tai dia diem can tim trong du lieu
                                bool flagTP = false;
                                bool flagQuan = false;
                                for (int t = 1; t < listInfoQuery.Count; t++)
                                {
                                    if (pTenTinhThanh.IndexOf(listInfoQuery[t].ToString()) > -1)
                                    {

                                        flagTP = true;
                                      
                                    }
                                    if (pTenQuan.IndexOf(listInfoQuery[t].ToString()) > -1)
                                    {
                                        flagQuan = true;
                                    }
                                }
                                if (nodeQuanHuyen.HasChildNodes == true&&(flagTP==true||flagQuan==true))
                                {
                                    
                                    for (int k = 0; k < nodeQuanHuyen.ChildNodes.Count; k++)//Duyệt qua các địa điểm
                                    {
                                        XmlNode nodeDiaDiem = nodeQuanHuyen.ChildNodes[k];
                                        string pTenDiaDiem = nodeDiaDiem.Attributes["tenDiaDiem"].Value.ToString().ToLower();
                                        String pLat = nodeDiaDiem.Attributes["Lat"].Value.ToString().ToLower();
                                        string pLng = nodeDiaDiem.Attributes["Lng"].Value.ToString().ToLower();
                                        String pLoaiThongTin = nodeDiaDiem.Attributes["LoaiThongBao"].Value.ToString().ToLower();
                                        DiaDiemDTO diaDiem = new DiaDiemDTO(pTenDiaDiem, pTenQuan, pLat, pLng, pLoaiThongTin);
                                        String thanhPho = nodeTinhThanh.Attributes["tenTinhThanh"].Value.ToLower();
                                        if (flagQuan == true)
                                        {
                                            listInfoTheoTPvaQuanHuyen.Add(diaDiem);
                                        }
                                        else
                                        {
                                            listInfo.Add(diaDiem);
                                        }
                                    }
                               }
                            }
                        }
                    }
                }
            }
            if (listInfoTheoTPvaQuanHuyen.Count > 0)
            {
                return listInfoTheoTPvaQuanHuyen;
            }
            return listInfo;
        }
        
    }
}
